package com.weetop.akly.activity.meessage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.weetop.akly.R;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.bean.SignInLogBean;
import com.weetop.akly.bean.MsgBean;
import com.weetop.akly.bean.SignRecordBean;
import com.weetop.akly.presenter.SignRecordPresent;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.utils.TimeUtils;
import com.weetop.akly.view.SignRecordView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SignDayRecordActivity extends BaseActivity<SignRecordPresent> implements SignRecordView {

    @BindView(R.id.tv_tl_title)
    TextView tvTlTitle;
    @BindView(R.id.tv_tl_right)
    TextView tvTlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rcy_record)
    RecyclerView rcyRecord;
    @BindView(R.id.srl_sign_record)
    SmartRefreshLayout srlSignRecord;
    private ArrayList<SignInLogBean> data = new ArrayList<>();
    private boolean isRefresh, isLoadMore;
    private int page = 1;

    @Override
    protected SignRecordPresent createPresenter() {
        return new SignRecordPresent(this, this);
    }

    private ArrayList<String> list = new ArrayList<>();
    private CommonRecyclerAdapter<SignInLogBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_day);
        ButterKnife.bind(this);
        setToolBar("打卡", "");
        intListView();
//        mPresenter.geSignRecord(page + "");
        mPresenter.getCheckinLog(page + "");
    }

    private void intListView() {
        rcyRecord.setItemAnimator(null);
        rcyRecord.setLayoutManager(new LinearLayoutManager(mActivity));
        srlSignRecord.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadMore = true;
                page++;
//                mPresenter.geSignRecord(page + "");
                mPresenter.getCheckinLog(page + "");
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                page = 1;
//                mPresenter.geSignRecord(page + "");
                mPresenter.getCheckinLog(page + "");
            }
        });
    }

    @Override
    public void getSignRecordSuccess(BaseModel<ArrayList<SignInLogBean>> o) {
//        data = o.getData();
//        showDataInList(data);
//        if (isRefresh) {
//            srlSignRecord.finishRefresh(true);
//            isRefresh = false;
//        } else if (isLoadMore) {
//
//            srlSignRecord.finishLoadMore(true);
//            isLoadMore = false;
//        }
    }

    @Override
    public void getSignRecordSuccess_old1(BaseModel<ArrayList<SignInLogBean>> o) {
//        data = o.getData();
//        showDataInList(data);
//        if (isRefresh) {
//            srlSignRecord.finishRefresh(true);
//            isRefresh = false;
//        } else if (isLoadMore) {
//
//            srlSignRecord.finishLoadMore(true);
//            isLoadMore = false;
//        }
        ArrayList<SignInLogBean> items = o.getData();
        showDataInList(items);
    }

    @Override
    public void getSignRecordSuccess_old(BaseModel<SignRecordBean> o) {

    }

    private void showDataInList(ArrayList<SignInLogBean> items) {
        if (adapter == null) {
            data.clear();
            data.addAll(items);
            adapter = new CommonRecyclerAdapter<SignInLogBean>(mActivity, R.layout.item_sign_record, data) {
                @Override
                public void onUpdate(BaseAdapterHelper helper, SignInLogBean item, int position) {
                    String timeState, signContext, signState;
                    String daka, zhuangtai;
                    // success,error,info,warning
//                    if (item.getIcon() == null) {
//                        helper.setImageResource(R.id.iv_sign_state, R.mipmap.icon_error);
//                    } else {
//                        switch (item.getIcon()) {
//                            case "success":
//                                helper.setImageResource(R.id.iv_sign_state, R.mipmap.icon_sign_success);
//                                break;
//                            case "info":
//                            case "warning":
//                                helper.setImageResource(R.id.iv_sign_state, R.mipmap.icon_doing);
//                                break;
//                            case "error":
//                            default:
//                                helper.setImageResource(R.id.iv_sign_state, R.mipmap.icon_error);
//                                break;
//                        }
//                    }
                    if (item.getStatus() == 1) {
                        helper.setImageResource(R.id.iv_sign_state, R.mipmap.icon_sign_success);
                    } else {
                        helper.setImageResource(R.id.iv_sign_state, R.mipmap.icon_error);
                    }
//                    timeState = BaseUtils.formatTime(Long.valueOf(item.getTimes()));
//                    signContext = item.getDetails();

                    if (item.getOn_or_clo() == 1) {
                        helper.setText(R.id.tv_sign_time, TimeUtils.fromLongToDate("MM-dd HH:mm",item.getStart_time()*1000));
                        daka = "上班";
                    } else {
                        helper.setText(R.id.tv_sign_time, TimeUtils.fromLongToDate("MM-dd HH:mm",item.getEnd_time()*1000));
                        daka = "下班";
                    }
                    if (item.getStatus() == 1) {
                        zhuangtai = "正常";
                    } else if (item.getStatus() == 2) {
                        zhuangtai = "";
                    } else if (item.getStatus() == 3) {
                        zhuangtai = "迟到";
                    } else if (item.getStatus() == 4) {
                        zhuangtai = "早退";
                    } else if (item.getStatus() == 5) {
                        zhuangtai = "旷工";
                    } else {
                        zhuangtai = "缺省";
                    }
                    helper.setText(R.id.tv_sign_context, item.getTimes() + "  " + daka + "一" + zhuangtai);

                }
            };
            rcyRecord.setAdapter(adapter);
            adapter.setOnItemClickListener((viewHolder, view, position) -> {
                if (BaseUtils.fastClick()) {
                    Intent intent = new Intent();
                    intent.putExtra("sign_day", data.get(position).getStart_time());
                    BaseUtils.toActivity(mActivity, SignDayDetailActivity.class, intent);
                }
            });
        } else {
            if (isRefresh) {
                data.clear();
                data.addAll(items);
                srlSignRecord.finishRefresh(true);
                isRefresh = false;
                adapter.replaceAll(data);
            } else if (isLoadMore) {
                data.addAll(items);
                adapter.addAll(items);
                isLoadMore = false;
            }
        }
        if (items == null || items.size() < 10) {
            srlSignRecord.finishLoadMoreWithNoMoreData();
        } else {
            srlSignRecord.finishLoadMore(true);
        }
    }


}
